package cn.task.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.task.po.Task;
import cn.task.po.TaskVo;
import cn.task.po.Taskmanger;
import cn.task.service.TaskMangerService;
import cn.task.service.TaskService;

@Controller
public class myTaskController {
	@Autowired
	private TaskMangerService taskMangerService;
	@Autowired
	private TaskService taskServer;
	@RequestMapping(value="/myTask")
	public @ResponseBody List<TaskVo> myTask(HttpSession session,HttpServletRequest request)
	{
		String tempId=null;
		String type=request.getParameter("type");
		ArrayList<TaskVo> taskList=new ArrayList<TaskVo>();
		String useraccount=(String) session.getAttribute("useraccount");
		Taskmanger taskmanger=taskMangerService.selectByAccount(useraccount);
		if(taskmanger!=null)
		{
			if("noReceive".equals(type))
			{
				tempId=taskmanger.getNoreceive();
			}
			if("hadReceive".equals(type))
			{
				tempId=taskmanger.getHadreceive();
			}
			if("inApply".equals(type))
			{
				tempId=taskmanger.getInapply();
			}
			if(tempId!=null&&tempId.length()>0)
			{
				String[] list=tempId.split("#");
				for(String temp:list)
				{
					Task task=taskServer.selectTaskById(temp);
					if(task!=null)
					{
						Date date=task.getReleasedate();
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
						String publishDate=sdf.format(date);
						TaskVo taskVo=new TaskVo();
						taskVo.setPublishDate(publishDate);
						String iseffective=task.getIseffective();
						if("1".equals(iseffective))
						{
							taskVo.setIsEffect("是");
						}
						else
						{
							taskVo.setIsEffect("否");
						}
						BeanUtils.copyProperties(task, taskVo);
						taskList.add(taskVo);
					}
				}
			}
			
		}
		return taskList;
	}
}
